﻿Module Module1

    Sub Main()
        Console.WriteLine("Welcome to MPQ Launcher")
        Console.WriteLine("Detecting .NET Version...")

        If My.Computer.FileSystem.DirectoryExists(Environ("systemroot") & "\Microsoft.NET\Framework\v4.0.30319") Then
            Console.WriteLine(".NET 4.0 found")

            Try
                If Not IO.File.Exists("MPQClient.exe") Then
                    Console.WriteLine("Extracting MPQClient...")
                    Dim MPQStream As New IO.FileStream("MPQClient.exe", IO.FileMode.Create)
                    With Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MPQLauncher.MPQClient.exe")
                        Dim Buffer(.Length) As Byte
                        .Read(Buffer, 0, .Length)
                        MPQStream.Write(Buffer, 0, .Length)
                        MPQStream.Close()
                    End With

                    Console.WriteLine("Extracting MPQClientConfig...")
                    Dim MPQConfigStream As New IO.FileStream("MPQClient.exe.config", IO.FileMode.Create)
                    With Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MPQLauncher.MPQClient.exe.config")
                        Dim Buffer(.Length) As Byte
                        .Read(Buffer, 0, .Length)
                        MPQConfigStream.Write(Buffer, 0, .Length)
                        MPQConfigStream.Close()
                    End With

                    Console.WriteLine("Extracting MPQService...")
                    Dim MPQServiceStream As New IO.FileStream("MPQService.dll", IO.FileMode.Create)
                    With Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MPQLauncher.MPQService.dll")
                        Dim Buffer(.Length) As Byte
                        .Read(Buffer, 0, .Length)
                        MPQServiceStream.Write(Buffer, 0, .Length)
                        MPQServiceStream.Close()
                    End With
                    IO.File.SetAttributes("MPQClient.exe.config", IO.FileAttributes.Hidden)

                    Console.WriteLine("Launching MPQClient...")
                    Dim MPQClient As Process = Process.Start("MPQClient.exe", "/f")
                    MPQClient.WaitForInputIdle()
                Else
                    Dim MPQClient As Process = Process.Start("MPQClient.exe")
                    MPQClient.WaitForInputIdle()
                End If

            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Console.ReadLine()
            End Try

        ElseIf My.Computer.FileSystem.DirectoryExists(Environ("systemroot") & "\Microsoft.NET\Framework\v3.5\") Then
            Console.WriteLine(".NET 3.5 found")

            Try
                If Not IO.File.Exists("MPQClient35.exe") Then
                    Console.WriteLine("Extracting MPQClient35...")
                    Dim MPQStream As New IO.FileStream("MPQClient35.exe", IO.FileMode.Create)
                    With Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MPQLauncher.MPQClient35.exe")
                        Dim Buffer(.Length) As Byte
                        .Read(Buffer, 0, .Length)
                        MPQStream.Write(Buffer, 0, .Length)
                        MPQStream.Close()
                    End With

                    Console.WriteLine("Extracting MPQClientConfig...")
                    Dim MPQConfigStream As New IO.FileStream("MPQClient35.exe.config", IO.FileMode.Create)
                    With Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MPQLauncher.MPQClient35.exe.config")
                        Dim Buffer(.Length) As Byte
                        .Read(Buffer, 0, .Length)
                        MPQConfigStream.Write(Buffer, 0, .Length)
                        MPQConfigStream.Close()
                    End With

                    Console.WriteLine("Extracting MPQService...")
                    Dim MPQServiceStream As New IO.FileStream("MPQService.dll", IO.FileMode.Create)
                    With Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MPQLauncher.MPQService.dll")
                        Dim Buffer(.Length) As Byte
                        .Read(Buffer, 0, .Length)
                        MPQServiceStream.Write(Buffer, 0, .Length)
                        MPQServiceStream.Close()
                    End With
                    IO.File.SetAttributes("MPQClient35.exe.config", IO.FileAttributes.Hidden)

                    Console.WriteLine("Launching MPQClient...")
                    Dim MPQClient As Process = Process.Start("MPQClient35.exe", "/f")
                    MPQClient.WaitForInputIdle()
                Else
                    Dim MPQClient As Process = Process.Start("MPQClient35.exe")
                    MPQClient.WaitForInputIdle()
                End If
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Console.ReadLine()
            End Try

        Else
            Console.WriteLine(".NET 3.5 or better required to run")
        End If
    End Sub

End Module
